package com.example.smartcity.ui.dashboard;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.WaterAccountBaseMessageBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.android.material.textfield.TextInputEditText;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class WaterActivity extends AppCompatActivity {

    private TextInputEditText type;
    private TextInputEditText ownerName;
    private TextInputEditText doorNo;
    private TextInputEditText balance;
    WaterAccountBaseMessageBean.DataBean dataBean;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 0:
                    type.setText(getIntent().getStringExtra("type"));
                    ownerName.setText(dataBean.getOwnerName());
                    doorNo.setText(String.valueOf(dataBean.getDoorNo()));
                    balance.setText(dataBean.getBalance());
                    break;
            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_water);
        type = findViewById(R.id.type);
        ownerName = findViewById(R.id.ownerName);
        doorNo = findViewById(R.id.doorNo);
        balance = findViewById(R.id.balance);
        initView();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/householder/" + getSharedPreferences("selfMessage",MODE_PRIVATE).getString("userID",null);
        OkHttpUtils.getCall(newUrl,new File(getBaseContext()).readFile("Token")).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("TAG", "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                WaterAccountBaseMessageBean bean = new Gson().fromJson(response.body().string(),WaterAccountBaseMessageBean.class);
                dataBean = bean.getData();
                Message message = new Message();
                message.what = 0;
                handler.sendMessage(message);
            }
        });
    }
}